<?php
$timeSlotInfoStr = Yii::t('BWeSport', 'TimeSlot Info');
$propertyStr = Yii::t('BWeSport', 'Property');
$contentStr = Yii::t('BWeSport', 'Content');
$activeTimeOfTimeSlotStr = Yii::t('BWeSport', 'ActiveTimeOfTimeSlot');
$expireTimeOfTimeSlotStr = Yii::t('BWeSport', 'ExpireTimeOfTimeSlot');
$offsetOfBookingStartTimeStr = Yii::t('BWeSport', 'OffsetOfBookingStartTime');
$OffsetOfBookingEndTime = Yii::t('BWeSport', 'OffsetOfBookingEndTime');
$timeSlotDescStr = Yii::t('BWeSport', 'TimeSlot Desc');
$timeSlotStartTimeStr = Yii::t('BWeSport', 'TimeSlot StartTime');
$timeSlotLengthStr = Yii::t('BWeSport', 'TimeSlotLength');
$timeSlotSplitTimesStr = Yii::t('BWeSport', 'TimeSlotSplitTimes');
$timeSlotRepeatTypeStr = Yii::t('BWeSport', 'TimeSlotRepeatType');
$closeStr = Yii::t('BWeSport', 'Close');
$saveTimeSetUpStr = Yii::t('BWeSport', 'Save Time Set Up');
?>

<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-tooltip.js"/>
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-popover.js"/>

<script type="text/javascript">
    $(document).ready(function() {
        $("a[rel='tooltip']").tooltip({'placement': 'right', 'z-index': '3000'});
    });
</script>
</script>
<?php
$bookableMessage = new BWMessage($bookableId);
$timeSlotInputMedia = $bookableMessage->getInputMediaManager()->getInputMedia('timeSlot');
if (isset($timeSlotId) && $timeSlotId > 0) {
    $configMessage = new BWMessage($timeSlotId);
    $myTimeSlotId = $timeSlotId;
}
/*
  else {
  $configMessage = new BWMessage();
  $configMessage->name = uniqid('t');
  $configMessage->save();
  //re-calculate name
  $timeSlotName = 'inputableConfig' . $configMessage->getId();
  $configMessage->name = $timeSlotName;
  $configMessage->save();
  $timeSlotInputMedia->getMessageManager()->addMessage($configMessage);
  $myTimeSlotId = $configMessage->getId();
  }
 * 
 */

$url = $control->getAjaxLinkUrl(
        'addBookableTimeSlot', NULL, array(
    'timeSlotId' => $timeSlotId,
    'bookableId' => $bookableId,
    'eventId' => $eventId
        )
);
$updateId = $control->getAjaxContainerId();
?>

<script type="text/javascript">
    $(function() {
        $('#activeTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#expireTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#offsetOfBookingStartTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#offsetOfBookingEndTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
        $('#timeSlotStartTime-timepicker1').datetimepicker({
            language: 'pt-BR'
        });
//        $('#datetimepicker1').datetimepicker({
//          language: 'pt-BR'                   //该样式可以显示为：2014-07-16 22:45:03，需要修改时间样式：data-format="yyyy-MM-dd hh:mm:ss"
//        });
    });


// this script for collecting the form data and pass to the controller action and doing the on success validations
    function saveBookableTimeSlot() {

        var formData = new FormData($("#message-form")[0]);
        //var formElement = document.getElementById("message-form");
        //var formData = new FormData(formElement);

        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(data);
                //alert(data);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
                $("#myModal").modal("hide");
            },
            error: function() {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }

</script>

<?php
$model = new BDynamicFormModel();
$timeSlotInputMediaField = 'inputMedia_timeSlot';
$inputTypeOfTimeSlotField = 'inputType_timeSlot';
//$repeatTypeOfTimeSlotField = 'media_repeatType_inputType_timeSlot';
$activeTimeOfTimeSlotField = 'media_activeTime_inputType_timeSlot';
$expireTimeOfTimeSlotField = 'media_expireTime_inputType_timeSlot';
$offsetOfBookingCancelTimeField = 'media_bookingCancelTimeOffset_inputType_timeSlot';
$offsetOfBookingCancelTimeUnitField = 'media_bookingCancelTimeOffsetUnit_inputType_timeSlot';
$offsetOfBookingStartTimeField = 'media_bookingStartTimeOffset_inputType_timeSlot';
$offsetOfBookingStartTimeUnitField = 'media_bookingStartTimeOffsetUnit_inputType_timeSlot';
$offsetOfBookingEndTimeField = 'media_bookingEndTimeOffset_inputType_timeSlot';
$offsetOfBookingEndTimeUnitField = 'media_bookingEndTimeOffsetUnit_inputType_timeSlot';

$model->addField($timeSlotInputMediaField);
$model->addField($inputTypeOfTimeSlotField);
//$model->addField($repeatTypeOfTimeSlotField);
$model->addField($activeTimeOfTimeSlotField);
$model->addField($expireTimeOfTimeSlotField);
$model->addField($offsetOfBookingCancelTimeField);
$model->addField($offsetOfBookingCancelTimeUnitField);
$model->addField($offsetOfBookingStartTimeField);
$model->addField($offsetOfBookingStartTimeUnitField);
$model->addField($offsetOfBookingEndTimeField);
$model->addField($offsetOfBookingEndTimeUnitField);

if ($timeSlotInputMedia->getId() != NULL) {
    $timeSlotInputMediaId = $timeSlotInputMedia->getId();
    $inputType = $timeSlotInputMedia->getInputType();
    if ($inputType instanceof BWTimeSlotType) {
        $activeTimeMedia = $inputType->getMediaManager()->getMedia('activeTime');
        if ($activeTimeMedia != NULL)
            $model->$activeTimeOfTimeSlotField = $activeTimeMedia->getContent();
        $expireTimeMedia = $inputType->getMediaManager()->getMedia('expireTime');
        if ($expireTimeMedia != NULL)
            $model->$expireTimeOfTimeSlotField = $expireTimeMedia->getContent();
        $offsetOfBookingCancelTimeMedia = $inputType->getMediaManager()->getMedia('bookingCancelTimeOffset');
        if ($offsetOfBookingCancelTimeMedia != NULL)
            $model->$offsetOfBookingStartTimeField = $offsetOfBookingCancelTimeMedia->getContent();
        $offsetOfBookingCancelTimeUnitMedia = $inputType->getMediaManager()->getMedia('bookingCancelTimeOffsetUnit');
        if ($offsetOfBookingCancelTimeUnitMedia != NULL)
            $model->$offsetOfBookingCancelTimeUnitField = $offsetOfBookingCancelTimeUnitMedia->getContent();
        $offsetOfBookingStartTimeMedia = $inputType->getMediaManager()->getMedia('bookingStartTimeOffset');
        if ($offsetOfBookingStartTimeMedia != NULL)
            $model->$offsetOfBookingStartTimeField = $offsetOfBookingStartTimeMedia->getContent();
        $offsetOfBookingStartTimeUnitMedia = $inputType->getMediaManager()->getMedia('bookingStartTimeOffsetUnit');
        if ($offsetOfBookingStartTimeUnitMedia != NULL)
            $model->$offsetOfBookingStartTimeUnitField = $offsetOfBookingStartTimeUnitMedia->getContent();
        $offsetOfBookingEndTimeMedia = $inputType->getMediaManager()->getMedia('bookingEndTimeOffset');
        if ($offsetOfBookingEndTimeMedia != NULL)
            $model->$offsetOfBookingEndTimeField = $offsetOfBookingEndTimeMedia->getContent();
        $offsetOfBookingEndTimeUnitMedia = $inputType->getMediaManager()->getMedia('bookingEndTimeOffsetUnit');
        if ($offsetOfBookingEndTimeUnitMedia != NULL)
            $model->$offsetOfBookingEndTimeUnitField = $offsetOfBookingEndTimeUnitMedia->getContent();
    }
}

/*
  if ($timeSlotInputMediaId == 0) {

  //add inputMedia to post data
  echo $form->hiddenField($model, $timeSlotInputMediaField, array('value' => 'timeSlot inputMedia'));
  //add inputType to post data
  echo $form->hiddenField($model, $inputTypeOfTimeSlotField, array('value' => 'BWTimeSlotType'));
  }
 * 
 */
?>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3><?php echo $timeSlotInfoStr; ?></h3>
</div>
<div class="modal-body" >
    <table class="table table-striped">
        <colgroup>
            <col class="span4">
            <col class="span5">
        </colgroup>
        <thead>
            <tr>
                <th><?php echo $propertyStr; ?></th>
                <th><?php echo $contentStr; ?></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $activeTimeOfTimeSlotStr; ?>
                </td>
                <td>
                    <div id="activeTime-timepicker1" class="input-append">
                        <?php
                        echo CHtml::activeTextField($model, $activeTimeOfTimeSlotField, array('data-format' => "yyyy-MM-dd hh:mm:ss", 'type' => "text", 'placeholder' => '09:00:00', 'required' => true));
                        ?>
                        <span class="add-on">
                            <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                            </i>
                        </span>     

                    </div>
                    <a href="#" rel="tooltip" title="本项代表场地开放的起始时间." >说明</a>
                </td>
            </tr>
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $expireTimeOfTimeSlotStr; ?>
                </td>
                <td>
                    <div id="expireTime-timepicker1" class="input-append">
                        <?php
                        echo CHtml::activeTextField($model, $expireTimeOfTimeSlotField, array('data-format' => "yyyy-MM-dd hh:mm:ss", 'type' => "text", 'placeholder' => '17:00:00', 'required' => true));
                        ?>
                        <span class="add-on">
                            <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                            </i>
                        </span>
                    </div>
                    <a href="#" rel="tooltip" title="本项代表场地关闭的时间." >说明</a>
                </td>
            </tr>        
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $offsetOfBookingStartTimeStr; ?>
                </td>
                <td>
                    <div>
                        <?php
                        echo CHtml::activeTextField($model, $offsetOfBookingStartTimeField, array('type' => "text", 'placeholder' => Yii::t('BWeSport', 'by default is one day')));
                        ?>
                        <a href="#" rel="tooltip" title="本项代表用户最早可以提前预定场地的时间间隔. 例如它是7天，意味着下个星期一需要使用场地的用户最早只能在本周一进行预订." >说明</a>
                    </div>
                    <div>
                        <?php
                        echo chtml::activeDropDownList($model, $offsetOfBookingStartTimeUnitField, BWTimeSlotType::getTimeUnitList());
                        ?>
                    </div>

                </td>            
            </tr>
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $OffsetOfBookingEndTime; ?>
                </td>
                <td>
                    <div >
                        <?php
                        echo chtml::activeTextField($model, $offsetOfBookingEndTimeField, array('type' => "text", 'placeholder' => 'by default is end time of timeslot'));
                        ?>
                        <a href="#" rel="tooltip" title="本项代表用户最迟可以提前预定场地的时间间隔. 例如它是1天，意味着本星期六需要使用场地的用户最迟要在本周五进行预订." >说明</a>
                    </div>
                    <div>
                        <?php
                        echo CHtml::activeDropDownList($model, $offsetOfBookingEndTimeUnitField, BWTimeSlotType::getTimeUnitList());
                        ?>
                    </div>

                </td>            
            </tr>
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', '取消预订提前时间'; ?>
                </td>
                <td>
                    <div>
                        <?php
                        echo CHtml::activeTextField($model, $offsetOfBookingCancelTimeField, array('type' => "text", 'placeholder' => Yii::t('BWeSport', 'by default is one day')));
                        ?>
                        <a href="#" rel="tooltip" title="本项代表用户必须提前多少时间才能取消预订，默认的情况下，只要场地还没有开始使用，就可以取消预订" >说明</a>
                    </div>
                    <div>
                        <?php
                        echo chtml::activeDropDownList($model, $offsetOfBookingCancelTimeUnitField, BWTimeSlotType::getTimeUnitList());
                        ?>
                    </div>

                </td>            
            </tr>
            <?php
            //$inputableTimeSlotField = 'inputable_timeSlot_timeSlot' . $i;
            //$inputableConfigField = 'message_inputableConfig' . $myTimeSlotId . '_inputType_timeSlot';
            //add media for inputable value
            //$timeSlotStartTimeField = 'media_startTime_inputable_timeSlot_timeSlot' . $i;
            $timeSlotDescriptionField = 'media_description_message_inputableConfig';
            $timeSlotStartTimeField = 'media_startTime_message_inputableConfig';
            //$timeSlotLengthField = 'media_length_inputable_timeSlot_timeSlot' . $i;
            $timeSlotLengthField = 'media_length_message_inputableConfig';
            $timeSlotLengthUnitField = 'media_lengthUnit_message_inputableConfig';
            //$timeSlotSplitTimesField = 'media_splitTimes_inputable_timeSlot_timeSlot' . $i;
            $timeSlotSplitTimesField = 'media_splitTimes_message_inputableConfig';
            $timeSlotRepeatTypeField = 'media_repeatType_message_inputableConfig';

            $model->addField($timeSlotDescriptionField);
            $model->addField($timeSlotStartTimeField);
            $model->addField($timeSlotLengthField);
            $model->addField($timeSlotLengthUnitField);
            $model->addField($timeSlotSplitTimesField);
            $model->addField($timeSlotRepeatTypeField);

            if (isset($timeSlotId) && $timeSlotId > 0) {
                //add exist value into model to show in view
                $model->$timeSlotDescriptionField = $configMessage->getMediaManager()->getMedia('description')->getContent();
                $model->$timeSlotStartTimeField = $configMessage->getMediaManager()->getMedia('startTime')->getContent();
                $model->$timeSlotLengthField = $configMessage->getMediaManager()->getMedia('length')->getContent();
                $model->$timeSlotLengthUnitField = $configMessage->getMediaManager()->getMedia('lengthUnit')->getContent();
                $model->$timeSlotSplitTimesField = $configMessage->getMediaManager()->getMedia('splitTimes')->getContent();
            }
            ?>

            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $timeSlotDescStr; ?>
                </td>
                <td>
                    <?php
                    echo CHtml::activeTextField($model, $timeSlotDescriptionField, array('placeholder' => 'description for timeslot', 'required' => true));
                    ?>
                    <a href="#" rel="tooltip" title="本项代表对时间段的说明及描述，可以不填." >说明</a>
                </td>                
            </tr>    
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $timeSlotStartTimeStr; ?>
                </td>
                <td>
                    <div id="timeSlotStartTime-timepicker1" class="input-append">
                        <?php
                        echo CHtml::activeTextField($model, $timeSlotStartTimeField, array('data-format' => "yyyy-MM-dd hh:mm:ss", 'type' => "text", 'placeholder' => '09:00:00', 'required' => true));
                        ?>
                        <span class="add-on">
                            <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                            </i>
                        </span>
                    </div>
                    <a href="#" rel="tooltip" title="本项代表本时间段的起始时间." >说明</a>
                </td>                
            </tr>  
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $timeSlotLengthStr; ?>
                </td>
                <td>
                    <div>
                        <?php
                        echo CHtml::activeTextField($model, $timeSlotLengthField, array('placeholder' => '', 'required' => true));
                        ?>
                        <a href="#" rel="tooltip" title="本项代表场地的时间片长度. 例如场地每天从9:00-19:00开放，则它的长度是10小时." >说明</a>
                    </div>
                    <div>
                        <?php
                        echo CHtml::activeDropDownList($model, $timeSlotLengthUnitField, BWTimeSlotType::getTimeUnitList());
                        ?>
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $timeSlotSplitTimesStr; ?>
                </td>
                <td>
                    <?php
                    echo CHtml::activeTextField($model, $timeSlotSplitTimesField, array('required' => true));
                    ?>
                    <a href="#" rel="tooltip" title="本项代表场地的单位时间片可以使用的次数. 例如11人制足球每天从9:00-19:00开放(长度是10小时)，用户预订一个单位后要使用2小时，则该场地的拆分次数为10/2=5." >说明</a>
                </td>                
            </tr>   
            <tr>
                <td>
                    <?php echo '<nobr style="color: red;font-size:20px">*</nobr>', $timeSlotRepeatTypeStr; ?>
                </td>
                <td>
                    <?php
                    $repeatTypeList = BWTimeSlotType::getRepeatTypeList();
                    echo CHtml::activeDropDownList($model, $timeSlotRepeatTypeField, $repeatTypeList);
                    ?>
                    <a href="#" rel="tooltip" title="本项代表场地的重复周期." >说明</a>
                </td>                
            </tr>
        </tbody>
    </table>
</div>

<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo $closeStr; ?></button>
    <button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:saveBookableTimeSlot()"><?php echo $saveTimeSetUpStr; ?></button>
</div>

